/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.github.ngocnh210.TrueName;

import com.github.ngocnh210.TrueName.Events.TruenamePlayerListener;
import com.github.ngocnh210.TrueName.Util.Config;
import java.io.File;
import java.util.logging.Logger;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.Event.Priority;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;

/**
 *
 * @author NgocNH
 */
public class TrueName extends JavaPlugin {
    private static final Logger log = Logger.getLogger("Minecraft");
    private PluginDescriptionFile description = getDescription();
    private final TruenamePlayerListener playerListener = new TruenamePlayerListener(this);

    public static String pluginName, pluginVersion;

    @Override
    public void onEnable() {
        setupPluginInformation();
        File f = getFile("/config.yml");
        if (!f.exists()) {
            DefaultFile("config.yml");
        }
        new Config("config", getDataFolder() + "/", "config.yml");

        f = getFile("/vi.yml");
        if (!f.exists()) {
            DefaultFile("vi.yml");
        }
        new Config("language", getDataFolder() + "/", "vi.yml");

        PluginManager pm = getServer().getPluginManager();
        pm.registerEvent(Event.Type.PLAYER_LOGIN, playerListener, Priority.High, this);
        log.info("[" + pluginName + "] " + Config.system_version + " " + pluginVersion + " " + Config.system_enabled + "!");
    }

    @Override
    public void onDisable() {
        getServer().getScheduler().cancelTasks(this);
        log.info("[" + pluginName + "] " + Config.system_version + " " + pluginVersion + " " + Config.system_disabled + "!");
    }

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
        Player player = null;

        if(sender instanceof Player) {
            player = (Player) sender;
        }
        if(cmd.getName().equalsIgnoreCase("reload")){
            return true;
        }
        return false;
    }

    private void DefaultFile(String name) {
        File f = new File(getDataFolder() + "/", name);

        if (!f.exists()) {
            java.io.InputStream input = getClass().getResourceAsStream("/files/" + name);
            if (input != null) {
                java.io.FileOutputStream output = null;
                try {
                    output = new java.io.FileOutputStream(f);
                    byte[] buf = new byte[8192];
                    int length = 0;

                    while ((length = input.read(buf)) > 0) {
                        output.write(buf, 0, length);
                    }

                    System.out.println("[" + pluginName + "] Thiet lap cau hinh mac dinh cho " + name);
                } catch (Exception e) {                    
                } finally {
                    try {
                        if (input != null) {
                            input.close();
                        }
                    } catch (Exception e) {
                        System.out.println(e.getStackTrace());
                        System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName());
                        System.out.println(Thread.currentThread().getStackTrace()[1].getLineNumber());
                        System.out.println(Thread.currentThread().getStackTrace()[1].getClassName());
                        System.out.println(Thread.currentThread().getStackTrace()[1].getFileName());
                    }
                    try {
                        if (output != null) {
                            output.close();
                        }
                    } catch (Exception e) {
                    }
                }
            }
        }
    }

    private File getFile(String fname)
    {
        File f = new File(getDataFolder() + File.separator + (String)fname);
        return f;
    }

    void setupPluginInformation() {
        pluginName = getDescription().getName();
        pluginVersion = getDescription().getVersion();
    }

    public String getPluginName() {
        return pluginName = getDescription().getName();
    }

    public String getVersion() {
        return pluginVersion = getDescription().getVersion();
    }
}
